import base from './base.js'

function req(obj){
	if(obj.isLoding && obj.url != "/game/pkInfo"){
		uni.showLoading({
			title: '加载中'
		})
	}
	return new Promise((resolve , reject ) => {
		let userInfo = uni.getStorageSync('user_info') // 用户信息
		let token = ''
		// if(userInfo){ // token 是否过期
		// 	let expires = userInfo.expiretime  // 缓存中的过期时间
		// 	let currentExpires = Math.round(new Date() / 1000) // 当前时间戳
		// 	if(expires) { // 判断是否存在过期时间
		// 		if(expires < currentExpires){ // 判断过期时间是否小于当前时间
		// 			// token 过期
		// 			uni.removeStorageSync('user_info')
		// 			token = ''
		// 		} else {
		// 			// token 未过期
		// 			token = userInfo?.token
		// 		}
		// 	} else { 
		// 		token = ''
		// 	}
		// }
		if(userInfo){
			token = userInfo?.token
		}
		const HOST = obj.base
		let method = obj.method || "GET"
		let url = HOST + obj.url || ''
		let data = obj.data || {}
		let responseType = obj.responseType || ""
		let header = obj.header || {
			'Content-Type': obj.contentType || 'application/json',
			// 'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImIxNjMzYjFiLWQ2ZGYtNGNhOS04MmQ1LTk5MWU3YTkxZTEwOSJ9.PxVnFOlHi0KAkAxuPTxwk_QkfjBVb5TTbrbzKMy7rdohZZlxVst92IL0iQiqa-X1VCe-p7Q-flbGbgVfIlrbrw' ,
			'Authorization': uni.getStorageSync('token') ? uni.getStorageSync('token'): '' ,
			'timestamp': Math.round(new Date())
		}
		let success = obj.success
		let fail = obj.fail
		
		function get_fn(data) {
			var arr = Object.keys(data);
			var str = "?";
			for (var i in data) {
				str += i;
				str += "=";
				str += data[i];
				str += "&";
			}
			data = str.substr(0, str.length - 1)
		}

		uni.request({
			url: url,
			data: data,
			method: method == 'get' || method == 'GET' ? get_fn(data) : method,
			header: header,
			responseType,
			success: (res => {
				
				if (obj.isLoding) {
					uni.hideLoading();
				}
				
				if(responseType){
					resolve(res)
					return
				}
				if(res.data.code === 200 || ['你已经在该PK中了','你已经在该PK中了,请邀请好友', 'pk人已经满了'].includes(res.data.msg) || url == 'https://api.map.baidu.com/reverse_geocoding/v3/'){ //请求成功状态码
					resolve(res)
				} else if(res.data.code == 401 ){ // 请求的状态码
					let page = getCurrentPages()
					let pages = page[page.length - 1]
					// token 过期或者 没有 token
					if(pages.$page.fullPath != '/pages/tabBar/home/home'){
						uni.showToast({
							title: '登陆过期或未登录',
							icon: 'none'
						})
					}
					
					uni.removeStorageSync('user_info')
					// console.log(pages.$page.fullPath,'ss9999')
					let query = []
					if(pages.$vm.options){
						for(let key in pages.$vm.options){
							query.push(`${key}=${pages.$vm.options[key]}`)
						}
					}
					console.log(pages.$page.fullPath,'登录')
					setTimeout(() => {
						if(pages.$page.fullPath !='/pages/tabBar/home/home'){
							uni.reLaunch({
								url: `/pages/login/login?route=${pages.route}${query.length > 0 ? '&' + query.join('&') : ''}`
							})
						}
					},2000)
				}else if(res.data.msg == '未添加提现信息' ) {
					uni.navigateTo({
						url: '/pages/my/accountCard/accountCard'
					})
				}else {
					// console.log('我走到这儿了111',res)
					setTimeout(()=>{
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration:2500
						})
					},1000)
					
				}
			}),
			fail: (err => {
				if (obj.isLoding) {
					uni.hideLoading();
				} else {
					uni.showToast({
						icon: 'none',
						title: '网络请求异常' + err,
						duration: 2000
					});
				}
				reject(err)
			})
		})
	})
}

export default req